<!DOCTYPE html>
<html ng-app="App">
<head>
    <title>System Info</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
    <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
    <script type="text/javascript" src="../../script/jquery.min.js"></script>
    <script type="text/javascript" src="../../script/semantic/semantic.min.js"></script>
</head>

<body>
    <div class="ui vertical masthead left aligned segment">
        <div class="ui container">
            <div>
                <h1 id="modelname"></h1>
            </div>
            <div>
                <div style="display:inline-block;" id="build"></div>
                <div style="float:right !important;" id="manufacturer"></div>
            </div>
            
        </div>
    </div>
    <div class="ui container">
        <br>
        <h3 class="ui header" locale="hwinfo/specifications">Specifications</h3>
        <div class="ui grid">
            <div class="six wide column" locale="hwinfo/model">Model :</div>
            <div class="ten wide column" id="model">Loading...</div>
            <div class="six wide column" locale="hwinfo/frequency">Frequency :</div>
            <div class="ten wide column" id="freq">Loading...</div>
            <div class="six wide column" locale="hwinfo/instruction">Instruction :</div>
            <div class="ten wide column" id="instruction">Loading...</div>
            <div class="six wide column" locale="hwinfo/hardware">Hardware :</div>
            <div class="ten wide column" id="hardware">Loading...</div>
            <div class="six wide column" locale="hwinfo/revision">Revision :</div>
            <div class="ten wide column" id="revision">Loading...</div>
            <div class="six wide column" locale="hwinfo/memory">Memory :</div>
            <div class="ten wide column" id="ram">Loading...</div>
            <div class="six wide column" locale="hwinfo/nic">NIC :</div>
            <div class="ten wide column" id="nic">Loading...</div>
            <div class="six wide column" locale="hwinfo/drive">Drive :</div>
            <div class="ten wide column" id="drive">Loading...</div>
            <div class="six wide column" locale="hwinfo/usb">USB :</div>
            <div class="ten wide column" id="usb">Loading...</div>
        </div>
        <br>
        <h3 class="ui header" locale="hwinfo/operation_time">Operation Time</h3>
        <div class="ui grid">
            <div class="six wide column" locale="hwinfo/boot_time">Boot Time:</div>
            <div class="ten wide column" id="startup">Loading...</div>
            <div class="six wide column" locale="hwinfo/up_time">Up Time :</div>
            <div class="ten wide column" id="operation">Loading...</div>
        </div>
    </div>
    
    <div class="ui divider"></div>
    <img class="ui small image" id="icon"></img>
    <br>
</body>
<script>
    var specLocale = NewAppLocale();
    $(document).ready(function() {
        specLocale.init("../locale/system_settings/spec.json", function(){
            specLocale.translate();
            initSystemInfo();
        });
        
    });

    function initSystemInfo() {
        $.getJSON("../../system/info/getArOZInfo?icon=true", function(data) {
            $("#modelname").html(data["DeviceModel"]);
            $("#icon").attr("src", "data:image/png;base64," + data["VendorIcon"]);
            $("#build").html(data["BuildVersion"]);
            $("#manufacturer").html(data["DeviceVendor"]);
        });
        $.getJSON("../../system/info/getCPUinfo", function(data) {
            $("#model").html(data["Model"]);
            $("#freq").html(data["Freq"] + "MHz");
            $("#instruction").html(data["Instruction"]);
            $("#hardware").html(data["Hardware"]);
            $("#revision").html(data["Revision"]);
        });
        $.get("../../system/info/getRAMinfo", function(data) {
            $("#ram").html(humanFileSize(data, false));
        });
        $.getJSON("../../system/info/ifconfig", function(data) {
            $("#nic").html("");
            $(data).each(function(index, item) {
                $("#nic").append(item + "<br>");
            });
        });
        $.getJSON("../../system/info/getDriveStat", function(data) {
            $("#drive").html("");
            $(data).each(function(index, item) {
                $("#drive").append(item["DriveLetter"] + " (" + item["FileSystem"] + ") " + humanFileSize(item["FreeSpace"], true) + "<br>");
            });
        });
        $.getJSON("../../system/info/usbPorts", function(data) {
            $("#usb").html("");
            $(data).each(function(index, item) {
                $("#usb").append(item + "<br>");
            });
        });

        $.getJSON("../../system/info/getRuntimeInfo", function(data){
            $("#startup").text(ao_module_utils.timeConverter(data.StartupTime));
            $("#operation").text(ao_module_utils.durationConverter(data.ContinuesRuntime));
            $("#operation").attr("startunix", data.StartupTime);
            accumulateOperationTime();
        });
    }

    function accumulateOperationTime(){
        setTimeout(function(){
            if ($("#operation").length > 0){
                var startUnix = $("#operation").attr("startunix");
                startUnix = parseInt(startUnix);
                var newOprTime = parseInt(Date.now()/1000) - startUnix
                $("#operation").text(ao_module_utils.durationConverter(newOprTime));
                accumulateOperationTime();
            }
        }, 1000);
    }

    //https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string/10420404
    function humanFileSize(bytes, si) {
        var thresh = si ? 1000 : 1024;
        if (Math.abs(bytes) < thresh) {
            return bytes + ' B';
        }
        var units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
        var u = -1;
        do {
            bytes /= thresh;
            ++u;
        } while (Math.abs(bytes) >= thresh && u < units.length - 1);
        return bytes.toFixed(1) + ' ' + units[u];
    }
</script>

</html>